package cn.thinkjoy.LawDataForExcel.quartz;
import cn.thinkjoy.LawDataForExcel.bean.ApunishNoticeInfo;
import cn.thinkjoy.LawDataForExcel.bean.CompanyInfo;
import cn.thinkjoy.LawDataForExcel.bean.PunishTypeInfo;
import cn.thinkjoy.LawDataForExcel.email.EmailMessage;
import cn.thinkjoy.LawDataForExcel.excel.PoIExcelInPut;
import cn.thinkjoy.LawDataForExcel.extractURL.HtmlUnitSearchWeb;
import cn.thinkjoy.LawDataForExcel.service.Impl.ApunishNoticeInfoServiceImpl;
import cn.thinkjoy.LawDataForExcel.service.Impl.CompanyInfoServiceImpl;
import cn.thinkjoy.LawDataForExcel.service.redis.SingleCommonCacheService;
import cn.thinkjoy.LawDataForExcel.util.UtilZipFile;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by thinkjoy on 2017/6/22.
 * 处理好的数据生成存入一个list集合中
 */public class QuartzApplicationToMysql {

    @Autowired
    CompanyInfoServiceImpl companyInfoService;
    @Autowired
    SingleCommonCacheService<CompanyInfo> redisCompanyInfo;
    @Autowired
    ApunishNoticeInfoServiceImpl apunishNoticeInfoService;

    public void execute(){
        System.out.println("开始执行");
        CompanyInfo companyInfo;
        String currentDate=new SimpleDateFormat("yyyy/MM/dd").format(new Date());
//        String currentDate="2017/06/29";
//        查找出当天的交通违法信息
        List<ApunishNoticeInfo> apunishNoticeInfos=apunishNoticeInfoService.findNoticesByDate(currentDate);
        if(apunishNoticeInfos.size()!=0){
            for(ApunishNoticeInfo apunishNoticeInfo:apunishNoticeInfos){
                //查找并赋值企业工商信息
                if(apunishNoticeInfo.getPtype().equals("企业/法人代表")){
                    String cname=apunishNoticeInfo.getAdminiName();
                    companyInfo=companyInfoService.findCompanyInfoByName(cname.trim());
                    if(companyInfo==null){
                        try {
                            String copyname=cname;
                            if(cname.indexOf("(")!=-1 && cname.indexOf(")")!=-1){
                                cname=cname.replaceAll("\\(","").replaceAll("\\)","");
                            }else if (cname.indexOf("（")!=-1 && cname.indexOf("）")!=-1){
                                cname=cname.replaceAll("（","").replaceAll("）","");
                            }
                            HtmlUnitSearchWeb htmlUnitSearchWeb=new HtmlUnitSearchWeb(cname.trim(),copyname);
                            companyInfo=htmlUnitSearchWeb.AdvanceSearch();
                            if(companyInfo!=null){
                                System.out.println("爬取到的数据："+companyInfo.toString());
                                companyInfoService.saveCompanyInfo(companyInfo);
                                redisCompanyInfo.setCacheObject(copyname,companyInfo);
                            }else {
                                System.out.println("数据爬取失败!");
                            }
                            Thread.sleep(20000);//休眠20s
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("休眠时间到");
                    }
                    if(companyInfo!=null){
                        apunishNoticeInfo.setRegistnumber(companyInfo.getRegistnumber());
                        apunishNoticeInfo.setOrganiznumber(companyInfo.getOrganiznumber());
                        apunishNoticeInfo.setLegalname(companyInfo.getLegalname());
                        apunishNoticeInfo.setCreditnumber(companyInfo.getCreditnumber());
                        apunishNoticeInfo.setTaxnumber(companyInfo.getTaxnumber());
                    }
                    apunishNoticeInfo.setIdNumber("");
                }
            }
          System.out.println("结果:");
            //处理好的数据list，ApunishNoticeInfo还包含其他的字段内容，对接时需要参考ApunishNoticeInfo类中的属性
          for(ApunishNoticeInfo apunishNoticeInfo:apunishNoticeInfos){
              apunishNoticeInfo.setPtype("罚款");
              System.out.println(apunishNoticeInfo.toString());
          }
        }else {
            System.out.println("当天没有数据");
        }
    }
}
